//
//  DDUserManager.m
//  Dingdong
//
//  Created by 程强 on 16/11/23.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDUserManager.h"
#import "DDIntegralSet.h"
#import "DDFavouriteSet.h"
#import "DDAreaSet.h"
#import "DDReceiptAddSet.h"
#import "DDCouponSet.h"

@implementation DDUserManager
- (DSASubject *)myIntegral:(NSInteger)type pageNumber:(NSInteger)page pageSize:(NSInteger)size
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/member/getPointsPage.do", ServerUrl];
    
    NSDictionary *body = @{@"type":@(type),@"pageNo":@(page),@"pageSize":@(size)};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB[@"data"];
        DDIntegralSet *set = [DDIntegralSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)myCollectionWithPageNumber:(NSInteger)page pageSize:(NSInteger)size
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/favorite/getPage.do", ServerUrl];
    
    NSDictionary *body = @{@"pageNo":@(page),@"pageSize":@(size)};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB[@"data"];
        DDFavouriteSet *set = [DDFavouriteSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)deleteCollection:(NSNumber *)favourite_id
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/favorite/cancel.do", ServerUrl];
    
    NSDictionary *body = @{@"id":favourite_id};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB[@"data"];
        [subject sendNext:rdic];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)addCollection:(NSNumber *)goods_id
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/favorite/add.do", ServerUrl];
    
    NSDictionary *body = @{@"id":goods_id};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB[@"data"];
        [subject sendNext:rdic];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)getArea:(NSString *)type
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/address/getchildren.do", ServerUrl];
    
    NSDictionary *body = @{@"id":type};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDAreaSet *set = [DDAreaSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)addReceiveAddress:(NSString *)name phone:(NSString *)phone country:(NSString *)country provinceId:(NSNumber *)provinceId
                           cityId:(NSNumber *)cityId regionId:(NSNumber *)regionId region:(NSString *)region city:(NSString *)city province:(NSString *)province addr:(NSString *)addr defAddr:(NSString *)defAddr add_id:(NSNumber *)add_id
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/address/add.do", ServerUrl];
    
    NSDictionary *body = nil;
    if (add_id) {
        body =@{@"name":name,@"phone":phone,@"country":country,@"provinceId":provinceId,@"cityId":cityId,@"regionId":regionId,@"region":region,@"city":city,@"province":province,@"addr":addr,@"defAddr":defAddr,@"id":add_id};
    }else{
        body = @{@"name":name,@"phone":phone,@"country":country,@"provinceId":provinceId,@"cityId":cityId,@"regionId":regionId,@"region":region,@"city":city,@"province":province,@"addr":addr,@"defAddr":defAddr};
    }
    
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDAreaSet *set = [DDAreaSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)receiveAddressListWithPage:(NSInteger)pageNo pageSize:(NSInteger)size
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/address/list.do", ServerUrl];
    
    NSDictionary *body = @{@"pageNo":@(pageNo),@"pageSize":@(size)};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDReceiptAddSet *set = [DDReceiptAddSet modelWithDictionary:rdic[@"data"]];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)setReceiveAddressDefault:(NSNumber *)region_id
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/address/setdef.do", ServerUrl];
    
    NSDictionary *body = @{@"id":region_id};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDAreaSet *set = [DDAreaSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)deleteReceiptAddress:(NSNumber *)add_id
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/address/del.do", ServerUrl];
    
    NSDictionary *body = @{@"addrId":add_id};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        [subject sendNext:rdic];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)updateUserInfoWithSex:(NSInteger)sex nickname:(NSString *)nickname  birthday:(NSString *)birthday type:(NSInteger)type
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/member/updateMemberInfo.do", ServerUrl];
    
    NSDictionary *body = nil;
    if (type == 1) {
        body = @{@"sex":@(sex)};
    }else if (type == 2){
        body = @{@"nickname":nickname};
    }else if (type == 3){
        body = @{@"birthday":birthday};
    }
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        [subject sendNext:rdic];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)updateUserPhoto:(UIImage *)image
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/member/updateMemberIcon.do", ServerUrl];
    
    NSData *data = UIImagePNGRepresentation(image);
    NSDictionary *body = nil;
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices postUploadFile:doVerifyurl sign:sign prams:nil thumbnail:data thumname:@"photo.png" success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        [subject sendNext:rdic];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)modifyPassword:(NSString *)oldPwd nPwd:(NSString *)npwd
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/member/modifyPwd.do", ServerUrl];
    
    NSDictionary *body = @{@"oldPassword":oldPwd,@"newPassword":npwd};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        [subject sendNext:rdic];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)getCouponListByType:(NSInteger)type page:(NSInteger)page size:(NSInteger)size
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/bouns/getbouns.do", ServerUrl];
    
    NSDictionary *body = @{@"bonustype":@(type),@"pageNo":@(page),@"pageSize":@(size)};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDMyCouponSet *set = [DDMyCouponSet modelWithDictionary:rdic[@"data"]];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
@end
